package com.base.helper;

import com.base.comm.CommUrls;
import com.base.utils.Md5Encrypt;

import javax.servlet.http.HttpServletRequest;

public class CPHelper {

	private static String appid = "52682574"; // appid
//	private static String maxtime; // 最大通话时长 120分钟

	/**
	 * Call
	 * 
	 * @param caller  主叫
	 * @param callees 被叫
	 * @return
	 */
	public static String getCall(HttpServletRequest request, String caller, String callees) {
		// 获取域名
		String path = request.getContextPath();
		String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort();
		// 回调地址
		String return_url = CommUrls.THISSERVER + "call/cpNotify";
		String sign = null; // MD5加密
		try {
			sign = Md5Encrypt.encrypt32("&appid=" + appid + "&callees=" + callees + "&caller=" + caller + "&return_url="
					+ return_url + "2c60d17f8b67aac58abd8ce9aa59f8a5");
		} catch (Exception e) {
			e.printStackTrace();
		}
		String url = "http://api.cqfenlan.com:8085/webapi.aspx?command=call&appid=" + appid + "&callees=" + callees + "&caller="
				+ caller + "&maxtime=0&params=&return_url=" + return_url + "&sign=" + sign + "";
		String json = null;
		try {
			json = WebHelper.GetData(url, null);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return json;
	}

//	public static String testCall(String caller, String callees, String bind_num) {
//		String sign = null; // MD5加密
//		try {
//			sign = Md5Encrypt.encrypt32("&appid=" + appid + "&callees=" + callees + "&caller=" + caller + "&return_url="
//					+ return_url + "&bind_num=" + bind_num + "4571D77D36877AF26115108B7B2A8F8B");
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//		String url = "http://101.132.253.123:89/api/apicall.ashx?appid=" + appid + "&callees=" + callees + "&caller="
//				+ caller + "&return_url=" + return_url + "&bind_num=" + bind_num + "&sign=" + sign + "";
//		String json = null;
//		try {
//			json = WebHelper.GetData(url, null);
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//		return json;
//	}

//	private static String[] telFirst = "134,135,136,137,138,139,150,151,152,157,158,159,130,131,132,155,156,133,153"
//			.split(",");
//
//	public static int getNum(int start, int end) {
//		return (int) (Math.random() * (end - start + 1) + start);
//	}
//
//	private static String getTel() {
//		int index = getNum(0, telFirst.length - 1);
//		String first = telFirst[index];
//		String second = String.valueOf(getNum(1, 888) + 10000).substring(1);
//		String third = String.valueOf(getNum(1, 9100) + 10000).substring(1);
//		return first + second + third;
//	}

//	public static String testCall(String caller, Integer n) {
//		int k = 0;
//		for (int i = 0; i < n / 5; i++) {
//			String[] num = { "02825054717", "02825054626", "02825054752", "02825054840", "02825054898" };
//			for (int j = 0; j < num.length; j++) {
//				String callees = getTel();
//				Call cl = GsonUtil.getObject(testCall(caller, callees, num[j]), Call.class);
//				if (cl.getCode() == 1) {
//					k++;
//				} else {
//					if (cl.getMessage().equals("主叫黑名单限制")) {
//						try {
//							Thread.sleep(600000); // 线程睡眠600秒
//						} catch (InterruptedException e) {
//							// TODO Auto-generated catch block
//							e.printStackTrace();
//						}
//						System.out.println("主叫黑名单限制，线程睡眠10分钟");
//						continue;
//					}
//					if (cl.getMessage().equals("被叫黑名单限制")) {
//						callees = getTel();
//						System.out.println("被叫黑名单限制，重新拉取被叫号码：" + callees);
//						i--;
//						continue;
//					}
//				}
//				System.out.println("已呼叫：" + (i * num.length + j + 1));
//				System.out.println("主叫号码为：" + caller);
//				System.out.println("回拨号码为：" + num[j]);
//				System.out.println("被叫号码为：" + callees);
//				System.out.println("呼叫时间是：" + Parameters.getNowToday());
//				System.out.println(cl.getMessage());
//				try {
//					Thread.sleep(60000); // 线程睡眠60秒-不接电话的时间为60秒
//				} catch (InterruptedException e) {
//					// TODO Auto-generated catch block
//					e.printStackTrace();
//				}
//			}
//		}
//		return "骚扰成功次数：" + k;
//	}
//
//	public static void main(String[] args) {
//		System.out.println(testCall("17815915548", 50));
//	}
}